package org.freespace.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freespace.model.Job;
import org.freespace.model.JobDetail;
import org.freespace.model.Testplan;
import org.freespace.service.IEnvironmentService;
import org.freespace.service.IJobDetailService;
import org.freespace.service.IJobLogService;
import org.freespace.service.IJobService;
import org.freespace.utils.InfluxDBUtils;
import org.freespace.utils.response.CommonResult;
import org.freespace.vo.JobVO;
import org.freespace.vo.TestPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import retrofit2.http.POST;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 作业表 前端控制器
 * </p>
 *
 * @author liuqi
 * @since 2023-05-08
 */
@Slf4j
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    IJobService jobService;
    @Autowired
    IJobLogService jobLogService;
    @Autowired
    IJobDetailService detailService;

    @Autowired
    IEnvironmentService environmentService;

    @Autowired
    InfluxDBUtils influxDBUtils;
    @PostMapping("createJob")
    public CommonResult createJob(@RequestBody Job job) throws Exception {
        jobService.createJob(job);
        return CommonResult.success();
    }

    @PostMapping("finish")
    public CommonResult finish(@RequestParam("jobId") String jobId,@RequestParam("ip") String ip,
                               @RequestParam("endTime") String endTime, @RequestParam("status") String status) throws Exception {
        jobService.finishJob(jobId, ip, endTime, status);
        return CommonResult.success();
    }

    @PostMapping(value = "list")
    public CommonResult list(@RequestBody JobVO vo) {
        IPage<Job> page = new Page<>(vo.getCurrentPage(), vo.getPageSize());
        Wrapper<Job> wrapper = Wrappers.<Job>lambdaQuery().eq(Job::getTestplanId, vo.getTestplanId())
                .and(wp -> wp.like(Job::getId, vo.getCondition())
                        .or().like(Job::getCreateBy, vo.getCondition())
                        .or().like(Job::getDescription, vo.getCondition()))
                .orderByDesc(Job::getId);
        IPage<Job> data = jobService.page(page, wrapper);
        log.info("{}", data);
        return CommonResult.success(data);
    }

    @GetMapping(value = "/getJobDetail/{testplanId}/{jobId}")
    public CommonResult getJobDetail(@PathVariable("testplanId") Integer testplanId, @PathVariable("jobId") Integer jobId) {
        return CommonResult.success(detailService.getDynamicInfo(testplanId, jobId));
    }

    @PostMapping(value = "/getTpsData")
    public CommonResult getTpsData(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getTpsData(vo));
    }

    @PostMapping(value = "/getResponseTimeData")
    public CommonResult getResponseTimeData(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getResponseTimeData(vo));
    }


    @PostMapping(value = "/getMaxResponseTimeData")
    public CommonResult getMaxResponseTimeData(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getMaxResponseTimeData(vo));
    }

    @PostMapping(value = "/getline90Data")
    public CommonResult getline90Data(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getline90Data(vo));
    }

    @PostMapping(value = "/getline95Data")
    public CommonResult getline95Data(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getline95Data(vo));
    }

    @PostMapping(value = "/getline99Data")
    public CommonResult getline99Data(@RequestBody JobVO vo) {
        return CommonResult.success(jobService.getline99Data(vo));
    }

    @GetMapping(value = "/envInfo/{jobId}")
    public CommonResult envInfo(@PathVariable("jobId") Integer jobId) {
        return CommonResult.success(environmentService.getJobEnvInfo(jobId));
    }

    @PostMapping(value = "testMaxTps")
    public CommonResult list(@RequestBody JobDetail vo) {
        return CommonResult.success(influxDBUtils.getMaxTps(vo.getLabel(), jobService.getById(vo.getJobId())));
    }

    @PostMapping(value = "getInfluxDbData")
    public CommonResult getInfluxDbData(@RequestBody JobDetail vo) {
        return CommonResult.success(influxDBUtils.getInfluxDbData(vo.getJobId()));
    }

    @GetMapping(value = "/aggregatereport/{testplanId}/{jobId}")
    public CommonResult aggregatereport(@PathVariable("testplanId") Integer testplanId, @PathVariable("jobId") Integer jobId) {
        return CommonResult.success(detailService.getAggregateReport(testplanId, jobId));
    }

    @GetMapping(value = "/env/{jobId}")
    public CommonResult jobEnvInfo(@PathVariable("jobId") Integer jobId) {
        return CommonResult.success(environmentService.getJobEnvInfo(jobId));
    }

    @GetMapping(value = "delete/{jobId}")
    public CommonResult del(@PathVariable("jobId") Integer jobId) {
        return CommonResult.success(jobService.removeById(jobId));
    }


    @GetMapping(value = "stop/{jobId}")
    public CommonResult stop(@PathVariable("jobId") Integer jobId) {
        jobService.stop(jobId);
        return CommonResult.success();
    }

    @PostMapping(value = "getJobLog")
    public CommonResult getJobLog(@RequestBody JobVO vo) {
        return CommonResult.success(jobLogService.getJobLog(vo.getIp(),vo.getJobId()+""));
    }


    @PostMapping(value = "getAssertJobLog")
    public CommonResult getAssertJobLog(@RequestBody JobVO vo) {
        return CommonResult.success(jobLogService.searchAssertJobLog(vo.getIp(), vo.getJobId()+"", vo.getSearchContent(), "1000"));
    }

    @GetMapping(value = "getPaltformlog/{jobId}")
    public CommonResult getPaltformlog(@PathVariable("jobId") Integer jobId) {
        return CommonResult.success(jobService.getPlatmforLog(jobId));
    }

    @GetMapping(value = "getIpList/{jobId}")
    public CommonResult getIpList(@PathVariable("jobId") Integer jobId) {
        return CommonResult.success(jobService.getIpList(jobId));
    }
}
